# encoding:utf-8
import sys, threading
sys.path.append('..')
from functools import wraps


def add_job(func, semp):

    def decor(*args, **kwargs):
        semp.acquire()
        func(*args, **kwargs)

    return decor


def create_job(semp):

    def wrapper(func):

        def _wrapper(*args, **kargs):
            semp.acquire()
            func(*args, **kargs)
            semp.release()

        return _wrapper

    return wrapper


def singleton(cls):
    instances = {}

    @wraps(cls)
    def getinstance(*args, **kw):
        if cls not in instances:
            instances[cls] = cls(*args, **kw)
        return instances[cls]

    return getinstance


class ThreadSingleton(object):
    __instant = None;
    __lock = threading.Lock();
   
    def __new__(self):
        if(ThreadSingleton.__instant == None):
            ThreadSingleton.__lock.acquire();
            if(ThreadSingleton.__instant == None):
                ThreadSingleton.__instant = object.__new__(self);
            ThreadSingleton.__lock.release()
        return ThreadSingleton.__instant
